//
//  BuildingDetailViewController.m
//  Kfangchan
//
//  Created by liran on 14/11/8.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "BuildingDetailViewController.h"
#import "UIImageView+WebCache.h"
#import "PageImageView.h"
#import "MapViewController.h"
#import "NewCustomerViewController.h"
#import "WXClient.h"
#import "ActiveViewController.h"
#import "BuildingShareViewController.h"

@interface BuildingDetailViewController ()

@property (strong, nonatomic) IBOutlet UIScrollView *scrollView;

@property (strong, nonatomic) IBOutlet UIView *headView;
@property (strong, nonatomic) IBOutlet UIImageView *imgUrlImageView;
@property (strong, nonatomic) IBOutlet UILabel *nameLabel;
@property (strong, nonatomic) IBOutlet UILabel *typeLabel;
@property (strong, nonatomic) IBOutlet UILabel *avgPriceLabel;

@property (strong, nonatomic) IBOutlet UIView *activeView;
@property (strong, nonatomic) IBOutlet UILabel *activeLabel;

@property (strong, nonatomic) IBOutlet UIView *addrView;
@property (strong, nonatomic) IBOutlet UILabel *addrLabel;
@property (strong, nonatomic) IBOutlet UIButton *addrBtn;

@property (strong, nonatomic) IBOutlet UIView *cooperateRuleView;

@property (strong, nonatomic) IBOutlet UIView *layoutView;
@property (strong, nonatomic) IBOutlet UIView *layoutImageView;

@property (strong, nonatomic) IBOutlet UIView *descrView;
@property (strong, nonatomic) IBOutlet UIWebView *descrWebView;

@property (strong, nonatomic) IBOutlet UIView *bottomView;

@property (strong, nonatomic) UIView *shareView;

@end

@implementation BuildingDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.scrollView.size = CGSizeMake(self.scrollView.width, [UIScreen mainScreen].bounds.size.height);
    
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"楼盘信息"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    [self initDetail];
    self.bottomView.origin = CGPointMake(0, [[UIScreen mainScreen] bounds].size.height - 110);
    
    [[SDWebImageManager sharedManager] downloadImageWithURL:[NSURL URLWithString:self.entity.shareImgUrl] options:SDWebImageRetryFailed progress:nil completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    NSString *height_str= [webView stringByEvaluatingJavaScriptFromString: @"document.body.offsetHeight"];
    int webHeight = [height_str intValue];
    if (webHeight < 10) {
        webHeight = 10;
    }
    self.descrWebView.height = webHeight;
    self.descrView.size = CGSizeMake(self.descrView.width, 21 + webHeight);
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.descrView.height, 320, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#D1CCCA"];
    [self.descrView addSubview:lineView];
    
    int scrollHeight = self.headView.height + self.activeView.height + self.addrView.height + self.cooperateRuleView.height + self.layoutView.height + self.descrView.height + 36 + 50;
    [self.scrollView setContentSize:CGSizeMake(320, scrollHeight + 60)];
}

#pragma mark -
#pragma mark 楼盘信息构建
- (void)initDetail {
    if (self.entity) {
        [self.imgUrlImageView sd_setImageWithURL:[NSURL URLWithString:self.entity.imgUrl] placeholderImage:[UIImage imageNamed:@"building_detail_default.png"]];
        self.nameLabel.text = self.entity.name;
        self.typeLabel.text = [NSString stringWithFormat:@"类型：%@", self.entity.type];
        self.avgPriceLabel.text = [NSString stringWithFormat:@"均价：%@", self.entity.avgPrice];
        
        if (self.entity.stickActiveId == 0) {
            self.activeView.size = CGSizeMake(self.activeView.width, 0);
            self.activeView.hidden = YES;
        } else {
            self.activeLabel.text = [NSString stringWithFormat:@"消息：%@", self.entity.stickActive.name] ;
        }
        self.addrView.origin = CGPointMake(0, self.activeView.origin.y + self.activeView.height);
        self.addrLabel.text = [NSString stringWithFormat:@"地址：%@", self.entity.addr];
        
        [self initCooperateRuleView];
        
        [self initLayoutView];
        
        [self initDescrView];
    }
}

#pragma mark 合作规则view构建
- (void)initCooperateRuleView {
    self.cooperateRuleView.origin = CGPointMake(0, self.addrView.origin.y + self.addrView.size.height + 9);
    
    UIView *delimitView = [[UIView alloc] initWithFrame:CGRectMake(0, 22, self.cooperateRuleView.width, 25)];
    delimitView.backgroundColor = [UIColor whiteColor];
    UILabel *delimitTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 10, 40, 15)];
    delimitTitleLabel.text = @"界定：";
    delimitTitleLabel.font = [UIFont systemFontOfSize:13];
    delimitTitleLabel.textAlignment = NSTextAlignmentRight;
    [delimitView addSubview:delimitTitleLabel];
    UILabel *delimitLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(42, delimitTitleLabel.origin.y, 53, 15)];
    delimitLabel1.text = @"客户界定";
    delimitLabel1.font = [UIFont systemFontOfSize:13];
    delimitLabel1.textColor = [UIColor blackColor];
    delimitLabel1.textAlignment = NSTextAlignmentLeft;
    [delimitView addSubview:delimitLabel1];
    UILabel *delimitLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(95, delimitTitleLabel.origin.y, 260, 15)];
    delimitLabel2.text = [NSString stringWithFormat:@"<%i分钟", self.entity.customerDefinitionNum];
    delimitLabel2.font = [UIFont systemFontOfSize:13];
    delimitLabel2.textColor = [UIColor redColor];
    delimitLabel2.textAlignment = NSTextAlignmentLeft;
    [delimitView addSubview:delimitLabel2];
    [self.cooperateRuleView addSubview:delimitView];

    int rewardViewHeight = 0;
//    if (self.entity.seeMoney > 0 || self.entity.orderMoney > 0) {
//        UIView *rewardView = [[UIView alloc] initWithFrame:CGRectMake(0, delimitView.origin.y + delimitView.height, self.cooperateRuleView.width, 0)];
//        rewardView.backgroundColor = [UIColor whiteColor];
//        
//        UILabel *rewardTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 40, 15)];
//        rewardTitleLabel.text = @"奖励：";
//        rewardTitleLabel.font = [UIFont systemFontOfSize:13];
//        rewardTitleLabel.textAlignment = NSTextAlignmentRight;
//        [rewardView addSubview:rewardTitleLabel];
//        rewardViewHeight += rewardTitleLabel.origin.y + rewardTitleLabel.height;
//        
//        int rewareLabelX = 42;
//        if (self.entity.seeMoney > 0) {
//            UILabel *rewardLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(rewareLabelX, 5, 53, 15)];
//            rewardLabel1.text = @"带看奖励";
//            rewardLabel1.font = [UIFont systemFontOfSize:13];
//            rewardLabel1.textColor = [UIColor blackColor];
//            rewardLabel1.textAlignment = NSTextAlignmentLeft;
//            [rewardView addSubview:rewardLabel1];
//            rewareLabelX += rewardLabel1.width + 1;
//            
//            UILabel *rewardLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(rewareLabelX, 5, 50, 15)];
//            rewardLabel2.text = [NSString stringWithFormat:@"%i元", self.entity.seeMoney];
//            rewardLabel2.font = [UIFont systemFontOfSize:13];
//            rewardLabel2.textColor = [UIColor redColor];
//            rewardLabel2.textAlignment = NSTextAlignmentLeft;
//            [rewardView addSubview:rewardLabel2];
//            rewareLabelX += rewardLabel2.width;
//        }
//        
//        if (self.entity.orderMoney > 0) {
//            UILabel *rewardLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(rewareLabelX, 5, 53, 15)];
//            rewardLabel1.text = @"排卡奖励";
//            rewardLabel1.font = [UIFont systemFontOfSize:13];
//            rewardLabel1.textColor = [UIColor blackColor];
//            rewardLabel1.textAlignment = NSTextAlignmentLeft;
//            [rewardView addSubview:rewardLabel1];
//            rewareLabelX += rewardLabel1.width + 1;
//            
//            UILabel *rewardLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(rewareLabelX, 5, 53, 15)];
//            rewardLabel2.text = [NSString stringWithFormat:@"%i元", self.entity.orderMoney];
//            rewardLabel2.font = [UIFont systemFontOfSize:13];
//            rewardLabel2.textColor = [UIColor redColor];
//            rewardLabel2.textAlignment = NSTextAlignmentLeft;
//            [rewardView addSubview:rewardLabel2];
//        }
//
//        rewardView.size = CGSizeMake(rewardView.width, rewardViewHeight);
//        [self.cooperateRuleView addSubview:rewardView];
//    }
    int rewareLabelY = 5;
    if (self.entity.rewardList.count > 0) {
        UIView *rewardView = [[UIView alloc] initWithFrame:CGRectMake(0, delimitView.origin.y + delimitView.height, self.cooperateRuleView.width, 0)];
        rewardView.backgroundColor = [UIColor whiteColor];
        UILabel *rewardTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 40, 15)];
        rewardTitleLabel.text = @"奖励：";
        rewardTitleLabel.font = [UIFont systemFontOfSize:13];
        rewardTitleLabel.textAlignment = NSTextAlignmentRight;
        [rewardView addSubview:rewardTitleLabel];
        rewardViewHeight += rewardTitleLabel.origin.y + rewardTitleLabel.size.height;
        
        int rewareLabelY = 5;
        for (int i = 0; i < self.entity.rewardList.count; i++) {
            UILabel *rewardLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, rewareLabelY, 260, 15)];
            rewardLabel.text = self.entity.rewardList[i];
            rewardLabel.font = [UIFont systemFontOfSize:13];
            if (self.entity.rewardRed == 1) {
                rewardLabel.textColor = [UIColor redColor];
            } else {
                rewardLabel.textColor = [UIColor blackColor];
            }
            rewardLabel.textAlignment = NSTextAlignmentLeft;
            [rewardView addSubview:rewardLabel];
            rewareLabelY += rewardLabel.size.height;
            rewardViewHeight += rewardLabel.size.height;
        }
        rewardViewHeight -= 15;
        rewardView.size = CGSizeMake(rewardView.width, rewardViewHeight);
        [self.cooperateRuleView addSubview:rewardView];
    }
    
    int commissionViewHeight = 0;
    UIView *commissionView = [[UIView alloc] initWithFrame:CGRectMake(0, 22 + delimitView.height + rewardViewHeight, self.cooperateRuleView.width, 0)];
    commissionView.backgroundColor = [UIColor whiteColor];
    
    UILabel *commissionTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(8, 5, 40, 15)];
    commissionTitleLabel.text = @"佣金：";
    commissionTitleLabel.font = [UIFont systemFontOfSize:13];
    commissionTitleLabel.textAlignment = NSTextAlignmentRight;
    [commissionView addSubview:commissionTitleLabel];
    commissionViewHeight += commissionTitleLabel.origin.y + commissionTitleLabel.size.height;
    
    UILabel *commissionLabel1 = [[UILabel alloc] initWithFrame:CGRectMake(42, 5, 53, 15)];
    commissionLabel1.text = @"结佣时间";
    commissionLabel1.font = [UIFont systemFontOfSize:13];
    commissionLabel1.textColor = [UIColor blackColor];
    commissionLabel1.textAlignment = NSTextAlignmentLeft;
    [commissionView addSubview:commissionLabel1];
    
    UILabel *commissionLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(95, 5, 215, 15)];
    commissionLabel2.text = [NSString stringWithFormat:@"<%i工作日", self.entity.payCommissionNum];
    commissionLabel2.font = [UIFont systemFontOfSize:13];
    commissionLabel2.textColor = [UIColor redColor];
    commissionLabel2.textAlignment = NSTextAlignmentLeft;
    [commissionView addSubview:commissionLabel2];
    
    if (self.entity.commissionRuleList.count > 0) {
        int commissionLabelY = commissionTitleLabel.origin.y + commissionTitleLabel.size.height + 2;
        for (int i = 0; i < self.entity.commissionRuleList.count; i++) {
            UILabel *commissionLabel = [[UILabel alloc] initWithFrame:CGRectMake(42, commissionLabelY, 260, 15)];
            commissionLabel.text = self.entity.commissionRuleList[i];
            commissionLabel.font = [UIFont systemFontOfSize:13];
            if (self.entity.commissionRuleRed == 1) {
                commissionLabel.textColor = [UIColor redColor];
            } else {
                commissionLabel.textColor = [UIColor blackColor];
            }
            commissionLabel.textAlignment = NSTextAlignmentLeft;
            [commissionView addSubview:commissionLabel];
            commissionLabelY += commissionLabel.size.height + 2;
            commissionViewHeight += commissionLabel.size.height + 2;
        }
    }
    commissionViewHeight += 10;
    commissionView.size = CGSizeMake(commissionView.width, commissionViewHeight);
    self.cooperateRuleView.size = CGSizeMake(self.cooperateRuleView.width, self.cooperateRuleView.height + delimitView.height + rewardViewHeight + commissionViewHeight);
    [self.cooperateRuleView addSubview:commissionView];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, self.cooperateRuleView.height, 320, 1)];
    lineView.backgroundColor = [UIColor colorWithHexString:@"#D1CCCA"];
    [self.cooperateRuleView addSubview:lineView];
}

#pragma mark 户型信息view构建
- (void)initLayoutView {
    self.layoutView.origin = CGPointMake(self.layoutView.origin.x, self.cooperateRuleView.origin.y + self.cooperateRuleView.height + 9);
    if (self.entity.layoutList.count < 1 && self.entity.trafficList.count < 1 && self.entity.effectList.count < 1) {
        self.layoutView.size = CGSizeMake(self.layoutView.width, 0);
        self.layoutView.hidden = YES;
        return;
    }
    
    NSArray *layoutList = self.entity.layoutList.array;
    NSMutableArray *imgs = [NSMutableArray array];
    NSMutableArray *titles = [NSMutableArray array];
    NSMutableArray *descrs = [NSMutableArray array];
    for (BuildingLayout *layout in layoutList) {
        [imgs addObject:layout.imgUrl];
        [titles addObject:layout.title];
        [descrs addObject:layout.descr];
    }
    PageImageView *pageView = [[PageImageView alloc] initWithFrame:CGRectMake(0, 0, 320, 200) WithImages:imgs AndTitles:titles AndDescr:descrs];
    pageView.aryLayoutImages = imgs;
    pageView.aryLayoutTitles = titles;
    pageView.aryLayoutDescrs = descrs;
    
    NSArray *trafficList = self.entity.trafficList.array;
    NSMutableArray *trafficImgs = [NSMutableArray array];
    NSMutableArray *trafficTitles = [NSMutableArray array];
    NSMutableArray *trafficDescrs = [NSMutableArray array];
    for (BuildingTraffic *traffic in trafficList) {
        [trafficImgs addObject:traffic.imgUrl];
        [trafficTitles addObject:traffic.title];
        [trafficDescrs addObject:traffic.descr];
    }
    pageView.aryTrafficImages = trafficImgs;
    pageView.aryTrafficTitles = trafficTitles;
    pageView.aryTrafficDescrs = trafficDescrs;
    
    NSArray *effectList = self.entity.effectList.array;
    NSMutableArray *effectImgs = [NSMutableArray array];
    NSMutableArray *effectTitles = [NSMutableArray array];
    NSMutableArray *effectDescrs = [NSMutableArray array];
    for (BuildingEffect *effect in effectList) {
        [effectImgs addObject:effect.imgUrl];
        [effectTitles addObject:effect.title];
        [effectDescrs addObject:effect.descr];
    }
    pageView.aryEffectImages = effectImgs;
    pageView.aryEffectTitles = effectTitles;
    pageView.aryEffectDescrs = effectDescrs;
    
    [self.layoutImageView addSubview:pageView];
}

#pragma mark 楼盘卖点view构建
- (void)initDescrView {
    self.descrView.origin = CGPointMake(self.descrView.origin.x, self.layoutView.origin.y + self.layoutView.height + 9);
//    self.descrView.origin = CGPointMake(self.descrView.origin.x, self.layoutView.origin.y + self.layoutView.height + 9);
    self.descrWebView.scrollView.scrollEnabled = NO;
    NSString *html = [NSString stringWithFormat:@"<!DOCTYPE html><html><head><meta name=\"viewport\" content=\"width=100%%, initial-scale=1, user-scalable=no\" /><style>body{padding:8px;margin:0;color:#555;line-height:24px;font-size:14px;} img{width:100%%;display:block;margin:8px 0;}</style></head><body>%@</body><html>", self.entity.descr];
    [self.descrWebView loadHTMLString:html baseURL:nil];
}

#pragma mark -
#pragma mark 活动列表
- (IBAction)active:(id)sender {
    ActiveViewController *avc = [[ActiveViewController alloc] init];
    avc.list = self.entity.activeList;
    [self.navigationController pushViewController:avc animated:YES];
}

#pragma mark -
#pragma mark - 地图页
- (IBAction)map:(id)sender {
    CLLocationCoordinate2D coor;
    coor.latitude = self.entity.latitude;
    coor.longitude = self.entity.longitude;
    MapViewController *mvc = [[MapViewController alloc] initWithCOOR:coor];
    
    [self.navigationController pushViewController:mvc animated:YES];
}

#pragma mark -
#pragma mark 分享相关
#pragma mark 分享view构建
- (void)initSharView {
    if (self.shareView == nil) {
        self.shareView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.shareView.backgroundColor = [UIColor clearColor];
        
        UIView *blackView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.shareView.width, self.shareView.height)];
        blackView.backgroundColor = [UIColor blackColor];
        blackView.alpha = 0.6;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideShareView)];
        [blackView addGestureRecognizer:tap];
        [self.shareView addSubview:blackView];
        
        UIView *shareBtnView = [[UIView alloc] initWithFrame:CGRectMake(0, self.shareView.size.height - 157 - 50, self.shareView.width, 157)];
        shareBtnView.backgroundColor = [UIColor whiteColor];
        
        UIButton *ysBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        ysBtn.frame = CGRectMake(13, 20, 62, 62);
        [ysBtn setImage:[UIImage imageNamed:@"building_detail_share_ys.png"] forState:UIControlStateNormal];
        [ysBtn addTarget:self action:@selector(ysShare:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *ysLabel = [[UILabel alloc] initWithFrame:CGRectMake(13, 90, 62, 15)];
        ysLabel.font = [UIFont systemFontOfSize:11];
        ysLabel.textAlignment = NSTextAlignmentCenter;
        ysLabel.text = @"演示";
        [shareBtnView addSubview:ysBtn];
        [shareBtnView addSubview:ysLabel];
        
        UIButton *smsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        smsBtn.frame = CGRectMake(90, 20, 62, 62);
        [smsBtn setImage:[UIImage imageNamed:@"building_detail_share_sms.png"] forState:UIControlStateNormal];
        [smsBtn addTarget:self action:@selector(smsShare:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *smsLabel = [[UILabel alloc] initWithFrame:CGRectMake(90, 90, 62, 15)];
        smsLabel.font = [UIFont systemFontOfSize:11];
        smsLabel.textAlignment = NSTextAlignmentCenter;
        smsLabel.text = @"短信";
        [shareBtnView addSubview:smsBtn];
        [shareBtnView addSubview:smsLabel];
       
        UIButton *wxBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        wxBtn.frame = CGRectMake(167, 20, 62, 62);
        [wxBtn setImage:[UIImage imageNamed:@"building_detail_share_wx.png"] forState:UIControlStateNormal];
        [wxBtn addTarget:self action:@selector(wxShare:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *wxLabel = [[UILabel alloc] initWithFrame:CGRectMake(167, 90, 62, 15)];
        wxLabel.font = [UIFont systemFontOfSize:11];
        wxLabel.textAlignment = NSTextAlignmentCenter;
        wxLabel.text = @"微信";
        [shareBtnView addSubview:wxBtn];
        [shareBtnView addSubview:wxLabel];
        
        UIButton *friendsBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        friendsBtn.frame = CGRectMake(244, 20, 62, 62);
        [friendsBtn setImage:[UIImage imageNamed:@"building_detail_share_friends.png"] forState:UIControlStateNormal];
        [friendsBtn addTarget:self action:@selector(friendsShare:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *friendsLabel = [[UILabel alloc] initWithFrame:CGRectMake(244, 90, 62, 15)];
        friendsLabel.font = [UIFont systemFontOfSize:11];
        friendsLabel.textAlignment = NSTextAlignmentCenter;
        friendsLabel.text = @"朋友圈";
        [shareBtnView addSubview:friendsBtn];
        [shareBtnView addSubview:friendsLabel];
        
        UILabel *remarkLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, shareBtnView.height - 40, shareBtnView.size.width, 20)];
        remarkLabel.font = [UIFont systemFontOfSize:11];
        remarkLabel.text = @"分享或演示的页面不带佣金信息";
        remarkLabel.textColor = [UIColor colorWithHexString:@"#8A8685"];
        remarkLabel.textAlignment = NSTextAlignmentCenter;
        [shareBtnView addSubview:remarkLabel];
        
        [self.shareView addSubview:shareBtnView];
        
        
    }
}

#pragma mark 显示分享
- (IBAction)showSharView {
    [self initSharView];
    
    [self.view addSubview:self.shareView];
}

#pragma mark 隐藏分享
- (void)hideShareView {
    [self initSharView];
    [self.shareView removeFromSuperview];
}

#pragma mark 演示分享
- (void)ysShare:(id)sender {
    NSString *url = self.entity.webUrl;
    BuildingShareViewController *bsv = [[BuildingShareViewController alloc] init];
    bsv.url = url;
    [self hideShareView];
    UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:bsv];
    [self presentViewController:nav animated:YES completion:nil];
}

#pragma mark 短信分享
- (void)smsShare:(id)sender {
    NSString *sms = [NSString stringWithFormat:@"%@ %@", self.entity.share, self.entity.webUrl];
    [self sendSMSPicker:[NSString stringWithFormat:@"%@", sms]];
}

- (void)sendSMSPicker:(NSString *)bodyOfMessage {
    if([MFMessageComposeViewController canSendText]) {
        MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc] init];
        controller.body = bodyOfMessage;
        controller.messageComposeDelegate = self;
        [self presentViewController:controller animated:YES completion:nil];
    }
}

- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result {
    if (result == MessageComposeResultCancelled) {
        //showAlertMessage(@"取消发送");
    }else if (result == MessageComposeResultFailed){
        showAlertMessage(@"发送失败");
    }else{
        showAlertMessage(@"发送成功");
    }
    
    [controller dismissViewControllerAnimated:YES completion:nil];
    [self hideShareView];
}


#pragma mark 微信分享
- (void)wxShare:(id)sender {
    [WXClient sharedClient];
    if([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        [[WXClient sharedClient] sendLinkContentWithURlStr:self.entity.webUrl andTitle:self.entity.name andImageURL:self.entity.shareImgUrl andDescription:self.entity.share andScene:0];
        return;
    }
    showAlertMessage(@"请安装微信客户端");
}

#pragma mark 朋友圈分享
- (void)friendsShare:(id)sender {
    [WXClient sharedClient];
    if([WXApi isWXAppInstalled] && [WXApi isWXAppSupportApi]) {
        [[WXClient sharedClient] sendLinkContentWithURlStr:self.entity.webUrl andTitle:self.entity.name andImageURL:self.entity.shareImgUrl andDescription:self.entity.share andScene:1];
        return;
    }
    showAlertMessage(@"请安装微信客户端");
}

#pragma mark 拨打电话
- (IBAction)call:(id)sender
{
    UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:nil message:[NSString stringWithFormat:@"拨打电话:%@", @"4006-999-068"] delegate:self cancelButtonTitle:@"取消" otherButtonTitles: @"确定", nil];
    [alterView show];
}

- (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {
    if (buttonIndex != alertView.cancelButtonIndex) {
        NSString *phone = @"4006999068";
        NSString *tel = [NSString stringWithFormat:@"tel://%@", phone];
    
        if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:tel]]) {
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:tel]];
        }
    }
}

#pragma mark -
#pragma mark 推荐客户
- (IBAction)recommendCustomer {
    NewCustomerViewController *nvc = [[NewCustomerViewController alloc] init];
    nvc.buildingId = self.entity.pkid;
    [self.navigationController pushViewController:nvc animated:YES];
}

#pragma mark -
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

@end
